package Leetcode;

import java.util.*;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class findAnagrams {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();
        int sl = s.length();
        int pl = p.length();
        if (pl > sl){
            return new ArrayList<>();
        }
        int[] sCount = new int[26];
        int[] pCount = new int[26];
        for (int i = 0; i < pl; i++) {
            ++sCount[s.charAt(i) - 'a'];
            ++pCount[p.charAt(i) - 'a'];
        }
        if (Arrays.equals(sCount, pCount)){
            result.add(0);
        }
        for (int i = 0; i < sl - pl; i++) {
            --sCount[s.charAt(i) - 'a'];
            ++sCount[s.charAt(i + pl) - 'a'];
            if (Arrays.equals(sCount, pCount)){
                result.add(i + 1);
            }
        }
        return result;
    }
}
